var app = getApp(),
  that;
Page({
  data: {
    winHeight: "", //窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    specialList: [],
    companyId: '',
    page: 1,
    limit: 10,
    currId: ''
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    console.log(e)
    this.setData({
      currentTab: e.detail.current,
    });
    that.special(that.data.specialList[e.detail.current].id)
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function(e) {
    console.log(e)
    var cur = e.target.dataset.current;
    var curId = e.target.dataset.id;
    if (this.data.currentTaB == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur,
        currId: curId
      })
      that.special(curId)
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function() {

    that = this
    //  高度自适应
    wx.getSystemInfo({
      success: function(res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
  },
  onShow: function() {
    that.getspecialList()
    // that.special()
  },
  getspecialList: function() {
    app.getData({
      url: app.api.getSpecialList(wx.getStorageSync("companyId"), 1, 10),
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              specialList: res.data.list,
              currId: res.data.list[0].id,
              cur: 0
            })
          }
          console.log('999' + res.data.list[that.data.currentTab].id)
          that.special(res.data.list[that.data.currentTab].id)

        }
      }
    })
  },
  special: function(e) {
    console.log('1111' + e)
    // that.setData({
    //   currentTab: e.detail.current
    // })
    app.getData({
      url: app.api.getSpecial(),
      method: 'POST',
      data: {
        companyId: wx.getStorageSync("companyId"),
        limit: 10,
        page: 1,
        memberId: wx.getStorageSync('bizUserInfo').id,
        specialClassId: e,
        // specialChoice:2
      },
      ok: function(res) {
        console.log(res)
        console.log(that.data.currentTab)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              special: res.data.list
            })
          }
        }
      }
    })
  },
  clickDrtail: function(e) {
    console.log(e.currentTarget.dataset.item.id)
    app.getData({
      url: app.api.getSpecialDetail(e.currentTarget.dataset.item.id, wx.getStorageSync('bizUserInfo').id),
      method: 'GET',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1 && res.data.data != null) {
            wx.navigateTo({
              url: 'specialDetail?item=' + encodeURIComponent(JSON.stringify(res.data.data)),
            })
          } else {
            wx.showToast({
              title: '内容不存在'
            })
          }
        }
      }
    })
    // if(){

    // }
    // wx.navigateTo({
    //   url: 'specialDetail?item=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item)),
    // })
  },


  footerTap: app.footerTap
})